/**
 * Copyright 2013 yangming.liu<liuyangming@gmail.com>.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, see <http://www.gnu.org/licenses/>.
 */
package org.bytesoft.openjta.resource;

import java.rmi.RemoteException;

import javax.transaction.HeuristicCommitException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;

import org.bytesoft.openjta.TransactionImpl;
import org.bytesoft.openjta.TransactionStatus;
import org.bytesoft.openjta.internal.RollbackRequiredException;
import org.bytesoft.openjta.internal.TransactionVote;
import org.bytesoft.openjta.xa.XidImpl;

public class RemoteTerminatorSkeleton implements Terminator {
	private final TransactionImpl transaction;

	public RemoteTerminatorSkeleton(TransactionImpl tx) {
		this.transaction = tx;
	}

	public TransactionVote xaPrepare() throws SystemException, RollbackRequiredException, RemoteException {
		int xaVote = transaction.xaPrepare();
		return new TransactionVote(xaVote);
	}

	public void xaCommit() throws HeuristicMixedException, HeuristicRollbackException, RollbackException,
			SecurityException, SystemException, RemoteException {
		// if (TransactionImpl.class.equals(transaction.getClass())) {
		// } else {
		// recovered-transaction
		// }
		transaction.xaCommit();
	}

	public void xaRollback() throws HeuristicMixedException, HeuristicCommitException, SystemException, RemoteException {
		transaction.xaRollback();
	}

	public void commit(boolean transferCoordinate) throws HeuristicMixedException, HeuristicRollbackException,
			RollbackException, SecurityException, SystemException, RemoteException {

		this.transaction.setCoordinateTaken(transferCoordinate);

		TransactionStatus transactionStatus = transaction.getTransactionStatus();
		if (transactionStatus.isRollbackOnly()) {
			try {
				this.rollback(transferCoordinate);
			} catch (HeuristicCommitException ex) {
				// ignore
			}
			throw new HeuristicRollbackException();
		} else if (transactionStatus.isStatusRollingBack(true)) {
			try {
				this.rollback(transferCoordinate);
			} catch (HeuristicCommitException ex) {
				// ignore
			}
			throw new HeuristicRollbackException();
		} else if (transactionStatus.isRolledBack()) {
			throw new RollbackException();
		} else if (transactionStatus.isCommitted()) {
			// do nothing
		} else {
			transaction.processCommit();
		}
	}

	public void rollback(boolean transferCoordinate) throws HeuristicMixedException, HeuristicCommitException,
			SystemException, RemoteException {

		this.transaction.setCoordinateTaken(transferCoordinate);

		TransactionStatus transactionStatus = transaction.getTransactionStatus();
		if (transactionStatus.isCommitted()) {
			throw new HeuristicCommitException();
		} else if (transactionStatus.isRolledBack()) {
			return;
		} else {
			transaction.processRollback();
		}
	}

	public void cleanup() throws SystemException, RemoteException {
		transaction.cleanup();
	}

	// public int ready() throws RemoteException {
	// int ready = Terminator.READY_OK;
	// List<TerminatorArchive> terminators = transaction.getTerminatorArchives();
	// for (int i = 0; i < terminators.size(); i++) {
	// TerminatorArchive holder = terminators.get(i);
	// Terminator terminator = holder.getTerminator();
	// try {
	// int remoteReady = terminator.ready();
	// if (remoteReady == Terminator.READY_OK) {
	// // ignore
	// } else if (remoteReady == Terminator.READY_PARTLY_OK) {
	// ready = Terminator.READY_PARTLY_OK;
	// } else {
	// ready = Terminator.READY_PARTLY_OK;
	// }
	// } catch (RemoteException rex) {
	// ready = Terminator.READY_PARTLY_OK;
	// } catch (RuntimeException rex) {
	// ready = Terminator.READY_PARTLY_OK;
	// }
	// }
	// return ready;
	// }

	public int getStatus() throws RemoteException {
		TransactionStatus transactionStatus = this.transaction.getTransactionStatus();
		return transactionStatus.getLastestStatus();
	}

	public XidImpl getXid() {
		throw new IllegalStateException();
	}

}
